package nongchang.kuaijiecaifu.com.ncapp_android.ui;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ezy.lite.util.Param;
import ezy.lite.util.UI;
import ezy.widget.adapter.TabsAdapter;
import nongchang.kuaijiecaifu.com.ncapp_android.Global;
import nongchang.kuaijiecaifu.com.ncapp_android.R;
import nongchang.kuaijiecaifu.com.ncapp_android.common.activity.BaseActivity;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.data.ChildCartManager;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.data.HomeCartManager;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.data.MultipleChildCartManager;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.event.CartCountChange;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.event.MutipleMoneyEvent;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.AllCart;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.HomeItemCart;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.fragment.basketdetailfragment.BasketGoodDetailFragment;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.fragment.basketmultipledetailfragment.BasketGoodMultipleDetailFragment;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.popup.SureOrderDialog;
import nongchang.kuaijiecaifu.com.ncapp_android.util.RxBus;

public class BasketMultipleDetailActivity extends BaseActivity {
    @BindView(R.id.orderby)
    TabLayout mTabOrderBy;
    @BindView(R.id.pager)
    ViewPager mViewPager;
    @BindView(R.id.txt_money)
    TextView txtMoney;
    private float totalMoney;
    private AllCart allCart;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multiple_basket_detail);
        ButterKnife.bind(this);
        allCart= Param.get(getIntent().getExtras());
        MultipleChildCartManager.cartList=new ArrayList<>();
        MultipleChildCartManager.cartList=allCart.itemList;
        Log.i("fjsiodfjsoif",allCart.itemList.size()+"size"+ChildCartManager.cartList.size());
        Fragment fragments[] = {new BasketGoodMultipleDetailFragment(), new BasketGoodDetailFragment(), new BasketGoodDetailFragment()};
        mViewPager.setAdapter(new TabsAdapter(getSupportFragmentManager(), fragments));
        mTabOrderBy.addTab(mTabOrderBy.newTab().setText("商品详情"), true);
        mTabOrderBy.addTab(mTabOrderBy.newTab().setText("烹饪做法"));
        mTabOrderBy.addTab(mTabOrderBy.newTab().setText("晒单记录"));
        mTabOrderBy.setOnTabSelectedListener(onTabSelectedListener);
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabOrderBy));

        RxBus.ofType(MutipleMoneyEvent.class).subscribe(event -> {
            txtMoney.setText("总计：￥" + event.money);
            totalMoney = event.money;
        });
    }

    TabLayout.OnTabSelectedListener onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            mViewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MultipleChildCartManager.cartList.clear();
    }

    @OnClick({R.id.btn_back, R.id.btn_add_cart})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_add_cart:
                AllCart item = new AllCart();
                item.itemList = ChildCartManager.cartList;
                item.type = 1;
                item.number = 1;
                item.unitId = allCart.unitId;
                item.isSelect = true;
                item.money = totalMoney;
                item.one_money = allCart.one_money;
                HomeCartManager.local.add(item);
                UI.showToast(this, "加入成功");
                break;
        }
    }
}
